import { makeAutoObservable } from 'mobx';
import { RolePermission,RolePermissionFormValues} from './types';
import { getRolePermissionList, createRolePermission, updateRolePermission, getInfo, deleteRolePermission, getActivePermission } from '../../../../../apis/backhub/rolePermission';
import { message } from 'antd';

class RolePermissionStore {
  functions: RolePermission[] = [];
  modalVisible = false; // 弹窗显示状态
  editingRolePermission: RolePermission | undefined = undefined; // 当前编辑的角色，undefined表示新建
  loading = false;
  permissionValues: any;
  permissionOptions = [];

  constructor() {
    makeAutoObservable(this);
  }

  // 获取角色列表
  fetchRolePermissions= async () => {
    this.loading = true;
    try {
      const response = await getRolePermissionList();
      this.functions = (response as any).data;
    } catch (error) {
      console.error('获取角色列表失败:', error);
    } finally {
      this.loading = false;
    }
  };
  
  // 获取用户
  getRolePermission = async (id: string) => {
    try {
      this.loading = true;
      const response = await getInfo(id);
      this.editingRolePermission =  (response as any).data;
      return this.editingRolePermission; // 返回获取到的用户数据
    } catch (error) {
      console.error('获取用户失败:', error);
      throw error;
    } finally {
      this.loading = false;
    }
  };


  // 获取角色列表
  getActivePermission = async () => {
    this.loading = true;
    try {
      this.permissionOptions=[];
      const response = await getActivePermission();
      this.permissionValues = (response as any).data;
      this.permissionValues && Object.entries(this.permissionValues).map(([roleId, config]) => (
        this.permissionOptions.push(config)
      ));
    } catch (error) {
      console.error('获取用户列表失败:', error);
    } finally {
      this.loading = false;
    }
  };

  // 显示创建弹窗
  showCreateModal = () => {
    this.editingRolePermission = undefined;
    this.modalVisible = true;
  };

  // 显示编辑弹窗
  showEditModal = (func: RolePermission) => {
    this.editingRolePermission = func;
    this.modalVisible = true;
  };

  // 隐藏弹窗
  hideModal = () => {
    this.modalVisible = false;
    this.editingRolePermission = undefined;
  };

  // 保存角色
  saveRolePermission = async (values: RolePermission) => {
    try {
      if (this.editingRolePermission && this.editingRolePermission.id) {
        values.roleId =  (values.roles as any)[0]; 
        values.permissionId = (values.permissions as any); 
        const response = await updateRolePermission(this.editingRolePermission.id, values);
        if((response as any)?.status){
            message.success('创建成功');
        }
      } else {
        const response = await createRolePermission(values);
        if((response as any)?.status){
            message.success('更新成功');
        }
      }
      this.hideModal();
      this.fetchRolePermissions();
    } catch (error) {
      console.error('保存角色失败:', error);
    }
  };


  // 删除角色
  deleteRolePermission = async (id: string) => {
    try {
      await deleteRolePermission(id);
      this.fetchRolePermissions();
    } catch (error) {
      console.error('删除角色失败:', error);
    }
  };
}

export const rolePermissionStore = new RolePermissionStore();